package com.mj.HashMap;

import com.mj.file.FileInfo;
import com.mj.file.Files;
import com.mj.linkedlist.tools.Asserts;
import com.mj.model.Key;
import com.mj.model.SubKey1;
import com.mj.model.SubKey2;


/**
 * @Author: LDeng
 * @Date: 2021-03-11 14:52
 */
public class Main {

    static void test01(){
        String string="jack";
        int len=string.length();
        int hashCode=0;
        for(int i=0;i<len;i++){
            char c = string.charAt(i);
            //hasCode=hasCode*31+c;
            hashCode=(hashCode<<5)-hashCode+c;
        }
        System.out.println(hashCode);
        System.out.println(string.hashCode());

    }


    static void test02(){
        Integer a=100;
        Float b=10.6f;
        Long c=1234l;
        Double d=10.99;
        String e="rose";

        System.out.println(Integer.hashCode(a));
        System.out.println(b.hashCode());
        System.out.println(c.hashCode());
        System.out.println(d.hashCode());
        System.out.println(e.hashCode());

    }

    static void test03(){
        Person p1=new Person(1,1.0f,"jack");
        Person p2=new Person(1,1.0f,"jack");
        Map<Object,Integer> map=new HashMap<>();
        map.put(p1,1);
        map.put(p2,33);
        map.put("jack",5);
        map.put("rose",9);
        map.put("jack",4);
        map.put(null,99);

        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
        System.out.println(p1.equals(p2));
        System.out.println(map.size());
        System.out.println(map.get("jack"));
        System.out.println(map.get(null));

        System.out.println(map.size());
        System.out.println(map.remove(p1));
        System.out.println(map.get(p1));
        System.out.println(map.size());
        System.out.println(map.remove("jack"));
        System.out.println(map.size());
        System.out.println(map.get("jack"));

        map.traversal(new Map.Visitor<Object, Integer>() {
            @Override
            public boolean visit(Object key, Integer value) {
                System.out.println(key+"_"+value);
                return false;
            }
        });

        Person p3=new Person(1,1.0f,"jack");
        System.out.println(map.containsKey(p1));
        System.out.println(map.containsKey(null));
        System.out.println(map.containsKey(p3));
        System.out.println(map.containsKey(222));
    }


    static void test04(){
        HashMap<Object,Integer> map=new HashMap<>();
        for (int i = 0; i <19; i++) {
            map.put(new Key(i),i);
        }
        map.put(new Key(4),100);
        map.put(new Key(19),99);
        System.out.println(map.get(new Key(19)));
        System.out.println(map.size());
        map.print();
        System.out.println("\n===================");
        System.out.println(map.get(new Key(4)));

    }

    static void test05(){
        HashMap<Object,Integer> map=new HashMap<>();
        SubKey1 key1 = new SubKey1(1);
        SubKey2 key2 = new SubKey2(1);
        map.put(key1,1);
        map.put(key2,2);
        System.out.println(key1.equals(key2));
        map.print();
    }


    static void test1Map(Map<String, Integer> map, String[] words) {
        Times.test(map.getClass().getName(), new Times.Task() {
            @Override
            public void execute() {
                for (String word : words) {
                    Integer count = map.get(word);
                    count = count == null ? 0 : count;
                    map.put(word, count + 1);
                }
                System.out.println(map.size()); // 17188
                int count = 0;
                for (String word : words) {
                    Integer i = map.get(word);
                    count += i == null ? 0 : i;
                    map.remove(word);
                }
                Asserts.test(count == words.length);
                Asserts.test(map.size() == 0);
            }
        });
    }

    static void test1() {
        String filepath = "C:\\Users\\LDeng\\Desktop\\rt\\java\\util";
        FileInfo fileInfo = Files.read(filepath, null);
        String[] words = fileInfo.words();

        System.out.println("总行数：" + fileInfo.getLines());
        System.out.println("单词总数：" + words.length);
        System.out.println("-------------------------------------");

        //test1Map(new TreeMap<>(), words);
        test1Map(new HashMap<>(), words);
       // test1Map(new LinkedHashMap<>(), words);
    }

    static void test2(HashMap<Object, Integer> map) {
        for (int i = 1; i <= 20; i++) {
            map.put(new Key(i), i);
        }
        for (int i = 5; i <= 7; i++) {
            map.put(new Key(i), i + 5);
        }
        Asserts.test(map.size() == 20);
        Asserts.test(map.get(new Key(4)) == 4);
        Asserts.test(map.get(new Key(5)) == 10);
        Asserts.test(map.get(new Key(6)) == 11);
        Asserts.test(map.get(new Key(7)) == 12);
        Asserts.test(map.get(new Key(8)) == 8);
    }

    static void test3(HashMap<Object, Integer> map) {
        map.put(null, 1); // 1
        map.put(new Object(), 2); // 2
        map.put("jack", 3); // 3
        map.put(10, 4); // 4
        map.put(new Object(), 5); // 5
        map.put("jack", 6);
        map.put(10, 7);
        map.put(null, 8);
        map.put(10, null);
        Asserts.test(map.size() == 5);
        Asserts.test(map.get(null) == 8);
        Asserts.test(map.get("jack") == 6);
        Asserts.test(map.get(10) == null);
        Asserts.test(map.get(new Object()) == null);
        Asserts.test(map.containsKey(10));
        Asserts.test(map.containsKey(null));
        Asserts.test(map.containsValue(null));
        Asserts.test(map.containsValue(1) == false);
    }

    static void test5(HashMap<Object, Integer> map) {
        for (int i = 1; i <= 20; i++) {
            map.put(new SubKey1(i), i);
        }
        map.put(new SubKey2(1), 5);
        Asserts.test(map.get(new SubKey1(1)) == 5);
        Asserts.test(map.get(new SubKey2(1)) == 5);
        Asserts.test(map.size() == 20);
        map.print();
        map.put(new SubKey1(1),14);
    }


    static void test4(HashMap<Object, Integer> map) {
        map.put("jack", 1);
        map.put("rose", 2);
        map.put("jim", 3);
        map.put("jake", 4);
        map.remove("jack");
        map.remove("jim");
        for (int i = 1; i <= 10; i++) {
            map.put("test" + i, i);
            map.put(new Key(i), i);
        }
        for (int i = 5; i <= 7; i++) {
            Asserts.test(map.remove(new Key(i)) == i);
        }
        for (int i = 1; i <= 3; i++) {
            map.put(new Key(i), i + 5);
        }
        //Asserts.test(map.size() == 19);
        Asserts.test(map.get(new Key(1)) == 6);
        Asserts.test(map.get(new Key(2)) == 7);
        Asserts.test(map.get(new Key(3)) == 8);
        Asserts.test(map.get(new Key(4)) == 4);
        Asserts.test(map.get(new Key(5)) == null);
        Asserts.test(map.get(new Key(6)) == null);
        Asserts.test(map.get(new Key(7)) == null);
        Asserts.test(map.get(new Key(8)) == 8);
        map.traversal(new Map.Visitor<Object, Integer>() {
            @Override
            public boolean visit(Object key, Integer value) {
                System.out.println(key + "_" + value);
                return false;
            }
        });
    }



    public static void main(String[] args) {
        // test1();
       // test2(new HashMap<>());
       // test3(new HashMap<>());
       // test4(new HashMap<>());
       // test5(new HashMap<>());

        test2(new LinkedHashMap<>());
        test3(new LinkedHashMap<>());
        test4(new LinkedHashMap<>());
        test5(new LinkedHashMap<>());


    }
}
